package vn.sunnet.game.SinhTon.screen;

import java.util.ArrayList;

import vn.sunnet.game.SinhTon.global.Assets;
import vn.sunnet.game.SinhTon.global.OverlapTester;
import vn.sunnet.game.SinhTon.global.Setting;
import vn.sunnet.game.SinhTon.object.SkillShopObject;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class ShopSkill extends BaseScreen {

	private Button btnBack, btnTiepTuc, btnMua, btnNapXu;

	private int select_Skill = 1;

	private float x_TouchDown, y_TouchDown;

	private ArrayList<SkillShopObject> arrSkillShop;

	boolean isNotEnoughCoin;

	public ShopSkill(MyGame game) {
		super(game);
		initButton();
		Assets.font1.setScale(0.7f);
		arrSkillShop = new ArrayList<SkillShopObject>();
		for (int i = 0; i < 3; i++) {
			arrSkillShop.add(new SkillShopObject(i + 1));
		}

	}

	// private void renderDebug() {
	// ShapeRenderer renderer = new ShapeRenderer();
	// renderer.setProjectionMatrix(camera.combined);
	// renderer.begin(ShapeType.Rectangle);
	//
	// for (SkillShopObject ss : arrSkillShop) {
	// renderer.rect(ss.bounds.x, ss.bounds.y, ss.bounds.width,
	// ss.bounds.height);
	// }
	//
	// renderer.end();
	// }

	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		camera.update();
		stage.act(delta);
		updateSelectOptionGun(delta);
		batch.begin();

		batch.draw(Assets.ttBgShopSkill, 0, 0);

		Assets.font.drawWrapped(batch, String.valueOf(Assets.coin), 200, 454,
				210, HAlignment.CENTER);

		drawSkillShop();
		// Ve thong bao khi khong du so xu de mua sung
//		if (isNotEnoughCoin)
//			batch.draw(Assets.ttNotEnoughCoin, 206.5f, 139.5f, 387, 201);
		batch.end();
		// renderDebug();
		stage.draw();
	}

	private void drawSkillShop() {
		for (SkillShopObject ss : arrSkillShop) {
			if (select_Skill == ss.getId()) {
				batch.draw(ss.getTrSkill_hover(), ss.bounds.x, ss.bounds.y,
						ss.bounds.width, ss.bounds.height);
				Assets.font1.drawWrapped(batch, ss.getDescription(), 507, 350,
						333);
				Assets.font1.drawWrapped(batch,
						String.valueOf(ss.getCoinValue()), 526, 192, 172,
						HAlignment.CENTER);
				// Assets.font.draw(batch, String.valueOf(ss.getCoinValue()),
				// 834, 320);
			} else
				batch.draw(ss.getTrSkill(), ss.bounds.x, ss.bounds.y,
						ss.bounds.width, ss.bounds.height);
		}
		// Neu skill da duoc mua roi thi ve anh nay
		if (Setting.getBoolean(select_Skill))
			batch.draw(Assets.ttBought, 195, 194, 120, 60);
	}

	private void updateSelectOptionGun(float time) {
		for (SkillShopObject ss : arrSkillShop) {
			if (OverlapTester.pointInRectangle(ss.bounds, x_TouchDown,
					y_TouchDown)) {
				select_Skill = ss.getId();
			}
		}
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		super.resize(width, height);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.BACK)
			game.setScreen(new ShopGun(game));
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		x_TouchDown = screenX * Assets.SCREEN_WIDTH / Gdx.graphics.getWidth();
		y_TouchDown = (Gdx.graphics.getHeight() - screenY)
				* Assets.SCREEN_HEIGHT / Gdx.graphics.getHeight();
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		isNotEnoughCoin = false;
		return false;
	}

	private void initButton() {
		btnBack = new Button(Assets.skin, "btnBack");
		btnBack.setBounds(14, 23, 77, 49);

		btnBack.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				game.setScreen(new ShopGun(game));
			}
		});

		btnTiepTuc = new Button(Assets.skin, "btnTiepTuc");
		btnTiepTuc.setBounds(611, 23, 173, 56);

		btnTiepTuc.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				game.setScreen(new PlayScreen(game));
			}
		});

		btnMua = new Button(Assets.skin, "btnMua");
		btnMua.setBounds(30, 191, 131, 44);

		btnMua.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				// Mua Skill
				if (!Setting.getBoolean(select_Skill)) {
					if (Assets.coin >= arrSkillShop.get(select_Skill - 1)
							.getCoinValue()) {
						if (Setting.getBoolean(Setting.SOUND))
							Assets.arrSoundInShop.get(1).play(1);
						isNotEnoughCoin = false;
						Assets.coin -= arrSkillShop.get(select_Skill - 1)
								.getCoinValue();
						Setting.setCoin(Assets.coin);
						// Assets.setBuySkill(select_Skill);
						Setting.putBoolean(select_Skill, true);
						game.request
								.trackBuyItem(arrSkillShop
										.get(select_Skill - 1).getCoinValue(),
										arrSkillShop.get(select_Skill - 1)
												.getId() + 10, Assets.map);
					} else {
						isNotEnoughCoin = true;
						game.dialog.notEnoughtCoin();
					}
				}
			}
		});
		btnNapXu = new Button(Assets.skin, "btnNapXu");
		btnNapXu.setBounds(660, 414, 109, 44);

		btnNapXu.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.arrSoundInShop.get(3).play(1);
				game.dialog.confirmBuyCoins();
			}
		});

		stage.addActor(btnBack);
		stage.addActor(btnTiepTuc);
		stage.addActor(btnMua);
		stage.addActor(btnNapXu);

	}
}
